# coding=utf-8
import json

import requests,os
from jiekouauto.check_res import check_res
from urllib import parse
import traceback
from commom.readtxt import writetxt
import urllib3
urllib3.disable_warnings()
def send(method, url, data, headers, id, yuqi,mu):
    '''
    发送请求
    :param method:请求方式
    :param url: 接口地址
    :param data: 请求参数
    :param headers: 请求头
    :param id :用例id
    :param yuqi: 预期结果
    :return:
    '''
    li = {}
    try:
        if method.lower() == 'get':
            # if data =='':
            #     r=requests.get(url,headers=headers,verify=False)
            # else:
            print(data)
            r = requests.get(url, params=data, headers=headers,verify=False)
            print('状态码：%s' % r.status_code)
            result = check_res(r.text, yuqi)
            li[id] = result
            try:
                res = json.loads(r.text)
            except:
                res=r.text
            # li['实际结果'] = ('msg:%s' % res['msg' + ',' + 'code:%s'] % res['code'])
            print(res)
            try:
                if  'msg' in res.keys():
                    li['实际结果'] = 'code:%s,msg:%s' % (res['code'],res['msg'])
                else:
                    li['实际结果']='code:%s'%res['code']
            except:
                li['实际结果'] = ''
            li['返回状态码'] = r.status_code
            li['原始返回'] = r.text
            # print(r.text)
        elif method.lower() == 'delete':
            # if data =='':
            #     r=requests.get(url,headers=headers,verify=False)
            # else:
            print(data)
            r = requests.delete(url, params=data, headers=headers,verify=False)
            print('状态码：%s' % r.status_code)
            result = check_res(r.text, yuqi)
            li[id] = result
            res = json.loads(r.text)
            # li['实际结果'] = ('msg:%s' % res['msg' + ',' + 'code:%s'] % res['code'])
            if  'msg' in res.keys():
                li['实际结果'] = 'code:%s,msg:%s' % (res['code'],res['msg'])
            else:
                li['实际结果']='code:%s'%res['code']
            li['返回状态码'] = r.status_code
            li['原始返回'] = r.text
            print(li['实际结果'])
            # print(r.text)
        elif method.lower() == 'post':
            if 'application/x-www-from-urlencoded' in headers['Content-Type']:
                data = parse.urlencode(data)
            if 'application/json'or 'application/json;charset=UTF-8' in headers['Content-Type']:
                data=json.dumps(data)
            # print(data,headers,url)
            r = requests.post(url, data=data, headers=headers,verify=False)
            print('状态码：%s' % r.status_code)
            result = check_res(r.text, yuqi)
            res = json.loads(r.text)
            if id == '登录':
                writetxt(os.path.join('E:\\接口自动化', '接口',mu, 'token.txt'), 'Bearer ' + res['data']['access_token'])
            li[id] = result
            if  'msg' in res.keys():
                li['实际结果'] = 'code:%s,msg:%s' % (res['code'],res['msg'])
            else:
                li['实际结果']='code:%s'%res['code']
            li['返回状态码'] = r.status_code
            li['原始返回'] = r.text
        elif method.lower()=='put':
            if 'application/x-www-from-urlencoded' in headers['Content-Type']:
                data = parse.urlencode(data)
            if 'application/json'or 'application/json;charset=UTF-8' in headers['Content-Type']:
                data=json.dumps(data)
            # print(data,headers,url)
            r = requests.put(url, data=data, headers=headers,verify=False)
            print('状态码：%s' % r.status_code)
            result = check_res(r.text, yuqi)
            res = json.loads(r.text)
            li[id] = result
            if  'msg' in res.keys():
                li['实际结果'] = 'code:%s,msg:%s' % (res['code'],res['msg'])
            else:
                li['实际结果']='code:%s'%res['code']
            li['返回状态码'] = r.status_code
            li['原始返回'] = r.text
        else:
            print('接口类型不是post或get')
            li[id] = False
    except Exception as a:
        print('[%s]用例接口调用失败：%s' % (id,traceback.format_exc() ))
        li[id] = False
        # li['实际结果'] = ('msg:%s' % res['msg' + ',' + 'code:%s'] % res['code'])
        # li['返回状态码'] = r.status_code
        # li['原始返回'] = r.text
    return li
